<?php

namespace register\model;

require_once ("./register/model/RegisterModel.php");

class userDAL {

	private $con;

	private $allUserArray = array();

	public function __construct() {

		$this -> con = mysqli_connect("127.0.0.1", "root", "", "users");
		if (mysqli_connect_errno($this -> con)) {
			echo "databasen gick åt helvete!" . mysqli_connect_errno();
		}

	}

	public function getAllUsers() {

		$result = mysqli_query($this -> con, "SELECT * FROM users");
		while ($row = mysqli_fetch_array($result)) {
			$this -> allUserArray[] = $row;
		}

	}

	public function saveUserToDB($user, $password) {

		mysqli_query($this -> con, "INSERT INTO `users` (`Username`, `Password`)
							VALUES ('$user', '$password')");

		mysqli_close($this -> con);

	}

	public function unavailableUsername($username) {
		
		foreach ($this->allUserArray as $user => $row) {
			if ($row[0] == $username) {
				return FALSE;
			}
		}
		return TRUE;

	}

}
